/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2010 huliqing, huliqing.cn@gmail.com
 *
 * This file is part of QFaces.
 * QFaces is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QFaces is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with QFaces.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 这个文件是QFaces的一部分。
 * 您可以单独使用或分发这个文件，但请不要移除这个头部声明信息.
 * QFaces是一个自由软件，您可以自由分发、修改其中的源代码或者重新发布它，
 * 新的任何修改后的重新发布版必须同样在遵守LGPL3或更后续的版本协议下发布.
 * 关于LGPL协议的细则请参考COPYING、COPYING.LESSER文件，
 * 您可以在QFaces的相关目录中获得LGPL协议的副本，
 * 如果没有找到，请连接到 http://www.gnu.org/licenses/ 查看并获取副本。
 *
 * - Author: Huliqing
 * - Contact: huliqing.cn@gmail.com
 * - License: GNU Lesser General Public License (LGPL)
 * - Blog and source code availability: http://www.huliqing.name/
 */

package name.huliqing.qfaces.test.web;

import java.util.TimeZone;
import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGroup;
import javax.faces.convert.DateTimeConverter;
import name.huliqing.qfaces.QFaces;
import name.huliqing.qfaces.component.Scroller;
import name.huliqing.qfaces.model.PageModel;
import name.huliqing.qfaces.model.PageParam;
import name.huliqing.qfaces.test.entity.Person;
import name.huliqing.qfaces.test.service.PersonService;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.html.HtmlInputText;
import name.huliqing.qfaces.component.html.HtmlColumn;

@ManagedBean
@RequestScoped
public class ScrollerBean extends Base {

    private UIData uiPersons;
    // 绑定到页面组件
    private Scroller scroller;

    /**
     * 组件所绑定的后台方法，非常简单，主要是获取数据：
     * 通过利用组件所传入的参数PageParam从后台数据库中获取数据。
     * PageParam主要包含了几个参数（start,pageSize,sortField,asc）
     * 通过这几个给定的参数从后台获取一页数据，然后封装成PageModel返
     * 回即可。
     * @param pp
     * @return pm
     */
    public PageModel<Person> loadData(PageParam pp) {
        PageModel<Person> pm = PersonService.findAllPersons(pp);
        return pm;
    }

    // ---- Action
    public void delete() {
        Person p = (Person) this.uiPersons.getRowData();
        PersonService.deletePerson(p);
    }

    public void addTestRecords() {
        PersonService.addTestRecords();
    }

    // 改变皮肤
    public void changeFace() {
        String face = this.scroller.getFace();
        if ("0".equals(face)) {
            this.scroller.setFace("1");
        } else if ("1".equals(face)) {
            this.scroller.setFace("0");
        }
    }

    // 改变位置
    public void changePos() {
        String pos = this.scroller.getDisplay();
        if ("bottom".equalsIgnoreCase(pos)) {
            this.scroller.setDisplay("top");
        } else if ("top".equalsIgnoreCase(pos)) {
            this.scroller.setDisplay("both");
        } else if ("both".equalsIgnoreCase(pos)) {
            this.scroller.setDisplay("bottom");
        }
    }

    // ---- Getter and Setter
    public UIData getUiPersons() {
        return uiPersons;
    }

    public void setUiPersons(UIData uiPersons) {
        this.uiPersons = uiPersons;
    }

    public Scroller getScroller() {
        return scroller;
    }

    public void setScroller(Scroller scroller) {
        this.scroller = scroller;
    }

    // ---- 页面代码显示
    @Override
    public String getHtmlCode() {
        return readFile("example/ui-scroller.xhtml");
    }

    @Override
    public String getJavaCode() {
        return readFile("example/code/ScrollerBean");
    }
    // ---- 以下代码，用于从后台生成表格
    private HtmlPanelGroup uiPanel;

    public HtmlPanelGroup getUiPanel() {
        return uiPanel;
    }

    public void setUiPanel(HtmlPanelGroup uiPanel) {
        this.uiPanel = uiPanel;
    }

    public void newGrid() {
        // 如果组件树中已经存在myscroller组件，则不再重复生成
        UIComponent temp = this.uiPanel.findComponent("myscroller");
        if (temp != null) {
            return;
        }
        // 生成scroller,并分配gid,这个gid用于关联dataTable与scroller
        String gid = "mygrid";
        Scroller s = new Scroller();
        s.setFor(gid);
        s.setVar("person"); // 这个var与DataTable的var意义相同
        s.setPageSize(5);
        s.setListenerAsExpression("#{scrollerBean.loadData}");
        // 给scroller分配一个id,用于检测并避免重复生成。
        s.setId("myscroller");
        // 设置排序列
        s.setSortFields("name,age,email,birthday");
        // 指定两个排序图标
        s.setSrcAsc("/example/res/image/imgAsc.jpg");
        s.setSrcDesc("/example/res/image/imgDesc.jpg");

        // 生成DataTable注意 id的设置
        HtmlDataTable g = new HtmlDataTable();
        g.setBorder(1);
        g.setId(gid);

        // column1, 生成各列
        HtmlColumn c1 = new name.huliqing.qfaces.component.html.HtmlColumn();
        HtmlOutputText h1 = new HtmlOutputText();
        HtmlInputText o1 = new HtmlInputText();
        c1.addChild(o1);
        c1.addFacet("header", h1);
        h1.setValue("姓名");
        o1.setValueExpression("value", QFaces.createValueExpression("#{person.name}", Object.class));

        // column2
        HtmlColumn c2 = new HtmlColumn();
        HtmlOutputText o2 = new HtmlOutputText();
        HtmlOutputText h2 = new HtmlOutputText();
        c2.addChild(o2);
        c2.addFacet("header", h2);
        h2.setValue("年龄");
        o2.setValueExpression("value", QFaces.createValueExpression("#{person.age}", Object.class));

        // column3
        HtmlColumn c3 = new HtmlColumn();
        HtmlOutputText o3 = new HtmlOutputText();
        HtmlOutputText h3 = new HtmlOutputText();
        c3.addChild(o3);
        c3.addFacet("header", h3);
        h3.setValue("邮件");
        o3.setValueExpression("value", QFaces.createValueExpression("#{person.email}", Object.class));

        // column4
        HtmlColumn c4 = new HtmlColumn();
        HtmlOutputText o4 = new HtmlOutputText();
        HtmlOutputText h4 = new HtmlOutputText();
        DateTimeConverter con = new DateTimeConverter();
        c4.addChild(o4);
        c4.addFacet("header", h4);
        o4.setConverter(con);
        h4.setValue("生日");
        o4.setValueExpression("value", QFaces.createValueExpression("#{person.birthday}", Object.class));
        con.setPattern("yyyy-MM-dd HH:mm:ss");
        con.setTimeZone(TimeZone.getTimeZone("GMT+8"));

        g.getChildren().add(c1);
        g.getChildren().add(c2);
        g.getChildren().add(c3);
        g.getChildren().add(c4);
        s.getChildren().add(g);

        this.uiPanel.getChildren().add(s);

    }

}
